<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('auth_model');
	}
	
	function login()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', 'Užívateľské meno', 'trim|required');
		$this->form_validation->set_rules('password', 'Heslo', 'trim|required|min_length[5]|max_length[12]');
		
		// ak prejde validaciou
		if ($this->form_validation->run())
		{			
			if ($this->auth_model->check())
			{
				// vytvorime session
				$data = $this->auth_model->getUserData($_POST['username']);
				$data['logged_in'] = true;
				
				$this->session->set_userdata($data);
				if ($this->session->userdata('type') == 3)
					redirect('admin');
				else if ($this->session->userdata('type') == 0)
					redirect('jedalnicek/vypis/'.$this->session->userdata('Rodne_cislo'));
				else
					redirect('pacienti');
			}
			else
			{
				$data['chyba'] = 'Tento užívateľ nie je zaregistrovaný v systéme.';
			}
		}
		else
		{
			// chyba
		}	
		$data['title'] = 'NIS - Login';
		$this->load->view('templates/header', $data);
		$this->load->view('project/login_view');	
		$this->load->view('templates/footer');
	}
	
	
	function logout()
	{
		$this->session->unset_userdata(array('logged_in'=>''));
		redirect('');
	}
	
}